package com.hongkzh.www.look.enterprise.userinfo.view.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hongkzh.www.R;
import com.hongkzh.www.look.enterprise.userinfo.model.bean.MediaProductBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Sandy on 2018/3/30.
 * 媒体简介 产品 adapter
 */

public class MediaProductAdapter extends RecyclerView.Adapter<MediaProductAdapter.ViewHolder> implements View.OnClickListener {


    private List<MediaProductBean.DataBean.ListBean> mProductList = new ArrayList<>();
    private MediaProductBean.DataBean dataBean = new MediaProductBean.DataBean();

    private int mPageNum;


    public MediaProductAdapter(int mPageNum) {
        this.mPageNum = mPageNum;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.media_product, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.tvMainTitle.setText(mProductList.get(position).getTitle());
        holder.tvSecondTitle.setText(mProductList.get(position).getSubtitle());
        Glide.with(holder.itemView).load(mProductList.get(position).getImgSrc()).into(holder.IVProduct);
        //TODO: 根据返回的状态显示已经预约和购买的展示
        String mProductShopState = mProductList.get(position).getState();
        if (!TextUtils.isEmpty(mProductShopState) && "1".equals(mProductShopState)) {
            holder.tvYuyueNum.setText("已预约 " + mProductList.get(position).getPreorderNum() + "人");
            holder.IVYuyueState.setImageResource(R.mipmap.watch_yuyue);
        } else if ("2".equals(mProductShopState)) {
            holder.tvYuyueNum.setText("已购买 " + mProductList.get(position).getOrderNum() + "人");
            holder.IVYuyueState.setImageResource(R.mipmap.watch_goumai);
        }

    }

    public void add(MediaProductBean mediaProductBean) {

        Log.i("getPageNum===", mediaProductBean.getData().getPageNumber() + "");
        Log.i("获取到当前的页面mPageNum====", mPageNum + "");


        if (null != mediaProductBean && !TextUtils.isEmpty(mediaProductBean.getData().getPageNumber() + "")) {
            int getPageNum = mediaProductBean.getData().getPageNumber();

            if (mPageNum == getPageNum) {
                mProductList = mediaProductBean.getData().getList();
            } else {
                for (int i = 0; i < mediaProductBean.getData().getList().size(); i++) {
                    mProductList.add(mediaProductBean.getData().getList().get(i));
                }
            }
        }
    }

    public void ClearList() {
        if (mProductList != null || mProductList.size() != 0) {
            mProductList.clear();
        }
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onClick();
    }


    public interface OnItemClickListener {
        void onClick();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public OnItemClickListener onItemClickListener;

    @Override
    public int getItemCount() {
        return mProductList == null ? 0 : mProductList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.IV_Product)
        ImageView IVProduct;
        @BindView(R.id.tv_main_title)
        TextView tvMainTitle;
        @BindView(R.id.tv_second_title)
        TextView tvSecondTitle;
        @BindView(R.id.tv_yuyue_Num)
        TextView tvYuyueNum;
        @BindView(R.id.IV_yuyue_State)
        ImageView IVYuyueState;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
